{extend name="main" /}

{block name="body"}
{include file="breadcrumb" /}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body" pad15="">


                        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
                            <ul class="layui-tab-title">
                                <li class="layui-this">支付测试</li>
                                <li>退款测试</li>
                                <li>支付记录</li>
                            </ul>
                            <div class="layui-tab-content">
                                <!-- 支付-->
                                <div class="layui-tab-item layui-show">
                                    <form action="ajax_pay" method="post" class="layui-form">
                                    {:xn_ConfigView($data,$labelList['pay'],'ceshi')}

                                        <div class="layui-form-item">
                                            <label class="layui-form-label">支付方式</label>
                                            <div class="layui-input-block">
                                                <input type="radio" name="type" value="1" title="微信" checked>
                                                <input type="radio" name="type" value="2" title="支付宝" >
                                            </div>
                                        </div>

                                        <div class="layui-form-item">
                                            <div class="layui-input-block">
                                                <button type="button" class="layui-btn" lay-submit="" lay-filter="pay">测试支付</button>
                                                <span>请在微信浏览器中测试！</span>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                                <!-- 退款-->
                                <div class="layui-tab-item">
                                    <form action="ajax_tuikuan" method="post"  class="layui-form">
                                        {:xn_ConfigView($data,$labelList['tuikuan'],'ceshi')}
                                        <div class="layui-form-item">
                                            <div class="layui-input-block">
                                                <button type="button" class="layui-btn" lay-submit="" lay-filter="orderQuery">查询订单</button>
                                                <button type="button" class="layui-btn" lay-submit="" lay-filter="tuikuan">测试退款</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>

                                <div class="layui-tab-item ">
                                    {include file="pay_demo/payment"}
                                </div>

                            </div>
                        </div>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}
{block name='js'}
<script>
    var form = layui.form,$ = layui.jquery ;
    //发起支付
    form.on('submit(pay)', function(data){
        console.log(data,'--data')
        _post('ajax_pay',data.field,function(res){
            if(data.field.type==1){
                //微信支付
                callpay(res.data);
            }else{
                $('body').append(res.data.alipay_res);
                // $("form").attr("target", "_blank");
            }
        })
        return false;
    });

    //查询订单
    form.on('submit(orderQuery)', function(data){
        _post('orderQuery',data.field,function(res){
            layer.msg(res.msg)
            var money = res.data.cash_fee||0;
            $('input[name="ceshi[money]"]').val(money/100);
        })
        return false;
    });

    //发起退款
    form.on('submit(tuikuan)', function(data){
        _post('ajax_tuikaun',data.field,function(res){
            layer.msg(res.msg,{icon:1})
        })
        return false;
    });

    //调用微信JS api 支付
    function jsApiCall(data)
    {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            data,
            function(res){
                if(res.err_msg=='get_brand_wcpay_request:ok'){
                    layer.msg("支付成功",{icon:1})
                }else{
                    layer.msg('支付取消');
                }
            }
        );
    }

    function callpay(data)
    {
        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', jsApiCall(data), false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', jsApiCall(data));
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall(data));
            }
        }else{
            jsApiCall(data);
        }
    }
</script>


<script src="/static/admin/js/layuiTableApi.js"></script>
<script>
    layui.use(['table','form','laytpl'],function(){
        //  <i class="layui-icon layui-icon-edit "></i>  编辑图标
        var table = layui.table,form = layui.form,laytpl  = layui.laytpl ;
        var cols = [
            {"field":"id","title":"ID"},
            {"field":"out_trade_no","title":"订单号"},
            {"field":"pay_trade_no","title":"交易单号"},
            {"field":"money","title":"订单金额"},
            {"field":"status_text","title":"状态"},
            {"field":"create_time_text","title":"创建时间"},
            {"title":"操作","templet":"#apiListBar","width":200}
        ];
        //表格初始化
        table.render(getLayuiInit('#test','getPaymentList.html',cols));

        //监听单行工具条
        //监听单行工具条
        table.on('tool(test)',(obj)=>{
            var data = obj.data;
            if(obj.event === 'del'){
                layuiApiDel(data,()=>{obj.del()})
            } else if(obj.event === 'edit'){
                //窗口编辑
                ajax_open('轮播图管理编辑','edit.html?id='+data.id,'650','620')
                //新页面编辑
                //[window.open]('edit.html?id='+data.id,'_self');
            }else if(obj.event =='tuikuan'){
                _post('ajax_tuikaun',{ceshi:{out_trade_no:data.out_trade_no,money:data.money}},function(res){
                    layer.msg(res.msg,{icon:1})
                })
                return false;
            }else if(obj.event =='query'){
                //查询订单信息
                _post('orderQuery',{ceshi:{out_trade_no:data.out_trade_no,money:data.money}},function(res){
                    layer.msg(res.msg,{icon:1})
                })
            }
        });
    })

</script>

{/block}